package com.example.nfcaccesssystem;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class NewUserActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.activity_new_user);
	    
	    ListView doorListView = (ListView)findViewById(R.id.doorsAvailableListView);
	    
	    doorListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	    
		final ArrayAdapter<Door> adapter = new ArrayAdapter<Door>(this, android.R.layout.simple_list_item_multiple_choice, Controller.getInstance().doorList);
		
		doorListView.setAdapter(adapter);
		
		Controller.getInstance().getUsers();

	}
	
	public void createUser(View v){
		EditText userNameText = (EditText)findViewById(R.id.nameText);
		EditText userAccessIdText = (EditText)findViewById(R.id.accessIdText);
		
		String newUserName = userNameText.getText().toString();
		String newUserAccessId = userAccessIdText.getText().toString();
		
		ListView doorListView = (ListView)findViewById(R.id.doorsAvailableListView);
		
		ArrayList<Door> doorList = new ArrayList<Door>();
		
		SparseBooleanArray selectedPos = doorListView.getCheckedItemPositions();
		
		for (int j = 0; j < doorListView.getAdapter().getCount(); j++){
	    	if (selectedPos.get(j)) {
	    		doorList.add((Door) doorListView.getAdapter().getItem(j));
	    	}
	    }
		
		Controller.getInstance().createUser(this, newUserName, newUserAccessId, doorList.toArray(new Door[0]));
	}
	
	public void cancelNewUser(View v){
		
	}
	
	public void setNewUser(boolean response) {
		if (response) {
			Intent i = new Intent(this, MainActivity.class);
			startActivity(i);
		}
	}

}
